package cn.bopomo.dealer.base.action;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import cn.bopomo.dealer.base.common.ServiceConfig;
import cn.bopomo.dealer.base.service.IOperatorService;
import cn.bopomo.dealer.base.util.ResultCode;
import cn.bopomo.dealer.base.vo.AuGroup;
import cn.bopomo.dealer.base.vo.GroupForm;
import cn.bopomo.dealer.base.vo.ModuleList;

public class GroupAddAction extends BaseAction {

	private String codeDescXMLPath = "/WEB-INF/code_description.xml";
	
	
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		// TODO Auto-generated method stub
		
		response.setContentType("text/html;charset=UTF-8");
		response.setHeader("Cache-control","no-cache");
		
		StringBuffer result = new StringBuffer();
		GroupForm groupForm = (GroupForm)form;
		if (!this.hasPermission(request)){
			result.append(ResultCode.OP_NO_PRIMITION).append("||").append("你没有该操作权限，请与管理员联系");
			response.getWriter().write(result.toString());
			return null;
		}
		
		IOperatorService os = (IOperatorService)this.findService(ServiceConfig.OPERATOR_SERVICE);
		AuGroup group = new AuGroup();
		BeanUtils.copyProperties(group,groupForm);
		
		if (groupForm.getModuleId() == null || "".equals(groupForm.getModuleId().toString())){
			result.append(ResultCode.OP_PARAMS_LOSE).append("||").append("请选择该权限组所属的功能模块");
			response.getWriter().write(result.toString());
			return null;
		}
		
		ModuleList module = os.getModule(groupForm.getModuleId());
		if (module == null){
			result.append(ResultCode.OP_PARAMS_LOSE).append("||").append("请选择该权限组所属的功能模块");
			response.getWriter().write(result.toString());
			return null;
		}
		
		
		group.setAddDate(new Date());
		group.setAddUid(Integer.parseInt(this.getUserId(request)));
		group.setModuleList(module);
		//String group_name = request.getParameter("group_name");
		//String group_authorize = request.getParameter("group_authorize");
		//group.setGroupName(group_name == null ? "" : group_name);
		//group.setGroupAuthorize(group_authorize == null ? "" : group_authorize);
		
		String check_result = os.checkGroupValidate(group);
		if (ResultCode.OP_SUCCESS.equals(check_result)){
			check_result = os.addGroup(group);
		}
		
		result.append(check_result).append("||").append(this.getCodeDescription(check_result,request.getSession().getServletContext().getResource(this.codeDescXMLPath).getPath()));
		response.getWriter().write(result.toString());
		return null;
	}

	
	
}
